﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using ESoft.DataAccess.Models;

namespace ESoft.DataAccess
{
    /// <summary>
    /// 基础仓储
    /// </summary>
    public interface IBaseReponsitory
    {
        /// <summary>
        /// 获取单个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        T Find<T>(object id) where T : EntityBase;

        /// <summary>
        /// 异步获取单个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<T> FindAsync<T>(object id) where T : EntityBase;

        /// <summary>
        /// 获取单个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        Task<T> FirstOrDefaultAsync<T>(Expression<Func<T, bool>> predicate) where T : EntityBase;

        /// <summary>
        /// 获取第一个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        T First<T>(Expression<Func<T, bool>> predicate) where T : EntityBase;

        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        T Add<T>(T entity) where T : EntityBase;

        /// <summary>
        /// 异步新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<T> AddAsync<T>(T entity) where T : EntityBase;

        /// <summary>
        /// 更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool Update<T>(T entity) where T : EntityBase;

        /// <summary>
        /// 异步更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<T> UpdateAsync<T>(T entity) where T : EntityBase;

        /// <summary>
        /// 批量更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entitys"></param>
        /// <returns></returns>
        bool UpdateBatch<T>(List<T> entitys) where T : EntityBase;

        /// <summary>
        /// 异步批量更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entitys"></param>
        /// <returns></returns>
        Task UpdateBatchAsync<T>(List<T> entitys) where T : EntityBase;

        /// <summary>
        /// 根据ID删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        bool Delete<T>(object id) where T : EntityBase;

        /// <summary>
        /// 根据ID删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<bool> DeleteAsync<T>(object id) where T : EntityBase;

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        bool Delete<T>(T entity) where T : EntityBase;

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<bool> DeleteAsync<T>(T entity) where T : EntityBase;

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entities"></param>
        /// <returns></returns>
        bool DeleteBatch<T>(IEnumerable<T> entities) where T : EntityBase;

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entities"></param>
        /// <returns></returns>
        Task<bool> DeleteBatchAsync<T>(IEnumerable<T> entities) where T : EntityBase;

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        bool Exist<T>(Expression<Func<T, bool>> predicate) where T : EntityBase;

        /// <summary>
        /// 总行数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        int Count<T>(Expression<Func<T, bool>> predicate) where T : EntityBase;

        /// <summary>
        /// 异步获取总行数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        Task<int> CountAsync<T>(Expression<Func<T, bool>> predicate) where T : EntityBase;

        /// <summary>
        /// 获取总值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="selector"></param>
        /// <returns></returns>
        int Sum<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, int>> selector) where T : EntityBase;

        /// <summary>
        /// 异步获取总值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="selector"></param>
        /// <returns></returns>
        Task<int> SumAsync<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, int>> selector)
            where T : EntityBase;

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        IEnumerable<T> FindAll<T>() where T : EntityBase;

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        Task<List<T>> FindAllAsync<T>() where T : EntityBase;

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        List<T> FindEntities<T>(Expression<Func<T, bool>> predicate) where T : EntityBase;

        /// <summary>
        /// 异步获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        Task<List<T>> FindEntitiesAsync<T>(Expression<Func<T, bool>> predicate) where T : EntityBase;

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page"></param>
        /// <param name="predicate"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        IQueryable<T> FindEntitiesByPage<T>(PageInput page, Expression<Func<T, bool>> predicate, SortVo sort)
            where T : EntityBase;

        /// <summary>
        /// 异步分页获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page"></param>
        /// <param name="predicate"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        Task<List<T>> FindEntitiesByPageAsync<T>(PageInput page, Expression<Func<T, bool>> predicate, SortVo sort)
          where T : EntityBase;
    }
}